//
//  Canal.m
//  SujetObserver
//
//  Created by Thomas Dupont on 05/01/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Canal.h"

#import "SensorProtocol.h"


@implementation Canal

#pragma mark -
#pragma mark Memory Management

- (id) init
{
	self = [super init];
	if (self != nil) {
		sensorData = nil;
	}
	return self;
}

- (void)dealloc
{
	[sensorData release];
	sensorData = nil;
	
	[super dealloc];
}

#pragma mark -
#pragma mark Accessors

@synthesize sensorData;
@synthesize speed;

#pragma mark -
#pragma mark Observer Implementation

- (void)update:(id<SensorProtocol>)subject
{
	float waitigDuration;
	if (speed == 0) {
		// Random speed
		srand(time(NULL));
		waitigDuration = (rand()%3) + 0.1;
	}
	else {
		waitigDuration = 20.f / speed;
	}
	
	// Simulate canal speed transmission
	[NSThread sleepForTimeInterval:waitigDuration];
	// Get the subject data
	SensorData* tempData = [[subject sensorData] retain];
	// Again simulate canal speed transmission
	[NSThread sleepForTimeInterval:waitigDuration];
	// Set the owned data, the observing implemented into Foundation will do the rest of the work
	self.sensorData = tempData;
	[tempData release];
	tempData = nil;
}

#pragma mark -
#pragma mark SensorProtocol Implementation for Proxy Pattern
- (void)setSensorValue:(NSNumber*)newValue
{
	
}
- (void)setSpreadStrategy:(id<SpreadProtocol>)aStrategy
{
	
}
- (id<SpreadProtocol>)spreadStrategy
{
	return nil;
}

#pragma mark -
#pragma mark NSObject Methods
- (NSString*)description
{
	return [NSString stringWithFormat:@"Canal Speed:%d",speed];
}


@end
